package com.jeeplus.modules.project.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.project.entity.BaseProjectInfo;
import com.jeeplus.modules.project.entity.KnotFinalMoney;
import com.jeeplus.modules.project.service.BaseProjectInfoService;
import com.jeeplus.modules.project.service.KnotFinalMoneyService;

/**
 * 项目结项经费决算Controller
 * @author jacky
 * @version 2018-01-27
 */
@Controller
@RequestMapping(value = "${adminPath}/project/knotFinalMoney")
public class KnotFinalMoneyController extends BaseController {
	@Autowired
	private KnotFinalMoneyService knotFinalMoneyService;
	@Autowired
	private BaseProjectInfoService baseProjectInfoService;
	
	@ModelAttribute
	public KnotFinalMoney get(@RequestParam(required=false) String id) {
		KnotFinalMoney entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = knotFinalMoneyService.get(id);
		}
		if (entity == null){
			entity = new KnotFinalMoney();
		}
		return entity;
	}
	
	/**
	 * 经费决算列表页面
	 */
	@RequestMapping(value = {"list", ""})
	public String list(KnotFinalMoney knotFinalMoney, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<KnotFinalMoney> page = knotFinalMoneyService.findPage(new Page<KnotFinalMoney>(request, response), knotFinalMoney); 
		model.addAttribute("page", page);
		model.addAttribute("knotFinalMoney", knotFinalMoney);
		return "modules/project/knot/knotFinalMoneyList";
	}

	/**
	 * 查看，增加，编辑结项经费决算表单页面
	 */
	@RequiresPermissions(value="project:knotFinalMoney:edit")
	@RequestMapping(value = "form")
	public String form(KnotFinalMoney knotFinalMoney, Model model) {
		model.addAttribute("knotFinalMoney", knotFinalMoney);
		return "modules/project/knot/knotFinalMoneyForm";
	}

	/**
	 * 保存结项经费决算
	 */
	@RequiresPermissions(value="project:knotFinalMoney:edit")
	@RequestMapping(value = "save")
	public String save(KnotFinalMoney knotFinalMoney, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, knotFinalMoney)){
			return form(knotFinalMoney, model);
		}
		if(!knotFinalMoney.getIsNewRecord()){//编辑表单保存
			KnotFinalMoney t = knotFinalMoneyService.get(knotFinalMoney.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(knotFinalMoney, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			knotFinalMoneyService.save(t);//保存
		}else{//新增表单保存
			knotFinalMoneyService.save(knotFinalMoney);//保存
		}
		addMessage(redirectAttributes, "保存结项经费决算成功");
		return "redirect:"+Global.getAdminPath()+"/project/knotFinalMoney/?repage";
	}
	
	/**
	 * 删除结项经费决算
	 */
	@RequiresPermissions("project:knotFinalMoney:edit")
	@RequestMapping(value = "delete")
	public String delete(KnotFinalMoney knotFinalMoney, RedirectAttributes redirectAttributes) {
		knotFinalMoneyService.delete(knotFinalMoney);
		addMessage(redirectAttributes, "删除结项经费决算成功");
		return "redirect:"+Global.getAdminPath()+"/project/knot/knotFinalMoney/?repage";
	}
	
	/**
	 * 批量删除结项经费决算
	 */
	@RequiresPermissions("project:knotFinalMoney:edit")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			knotFinalMoneyService.delete(knotFinalMoneyService.get(id));
		}
		addMessage(redirectAttributes, "删除结项经费决算成功");
		return "redirect:"+Global.getAdminPath()+"/project/knot/knotFinalMoney/?repage";
	}
	
	
	

}